package com.tajiang.business.setting.activity;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gprinter.aidl.GpService;
import com.gprinter.io.GpDevice;
import com.gprinter.service.GpPrintService;
import com.tajiang.business.R;
import com.tajiang.business.common.activity.BaseActivity;
import com.tajiang.business.common.utils.LogUtils;
import com.tajiang.business.common.utils.ToastUtils;
import com.tajiang.business.bluetooth.PrinterConnectActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/7/22.
 */
public class ChooseBlueTooth extends BaseActivity {

    public static final String CONNECT_STATUS = "connect.status";

    public static String EXTRA_DEVICE_ADDRESS = "device_address";

    @BindView(R.id.tv_device_name)
    TextView tvDeviceName;
    @BindView(R.id.tv_to_connect)
    TextView tvToConnect;
    @BindView(R.id.rl_connect_printer)
    RelativeLayout rlConnectPrinter;

    private GpService mGpService;
    private PrinterServiceConnection conn = null;

    class PrinterServiceConnection implements ServiceConnection {
        @Override
        public void onServiceDisconnected(ComponentName name) {

            LogUtils.i("TJA", "onServiceDisconnected() called");
            mGpService = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mGpService = GpService.Stub.asInterface(service);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }



    @Override
    protected void initTopBar() {
        setTitle("连接蓝牙打印机");
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_chooose_bluetooth_device);
        initPortParam();
        initMyView();
        connection();
    }

    @Override
    protected void initData() {

    }

    private void initMyView() {

    }

    private void initPortParam() {

    }

    private void connection() {
        conn = new PrinterServiceConnection();
        Intent intent = new Intent(this, GpPrintService.class);
        bindService(intent, conn, Context.BIND_AUTO_CREATE); // bindService
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (resultCode == Activity.RESULT_OK) {
                // Get the device MAC address
                String address = data.getExtras().getString(
                        EXTRA_DEVICE_ADDRESS);
                // fill in some parameters
                tvDeviceName.setText(address);
            }
        }
    }

    @OnClick(R.id.tv_to_connect)
    public void onConnectClick() {

    }

    @OnClick(R.id.rl_connect_printer)
    public void onClick() {
        if (mGpService == null) {
            ToastUtils.showShort("Print Service is not start, please check it");
            return;
        }
        Intent intent = new Intent(this,PrinterConnectActivity.class);
        boolean[] state = getConnectState();
        intent.putExtra(CONNECT_STATUS, state);
        startActivity(intent);
    }

    public boolean[] getConnectState() {
        boolean[] state = new boolean[GpPrintService.MAX_PRINTER_CNT];
        for (int i = 0; i < GpPrintService.MAX_PRINTER_CNT; i++) {
            state[i] = false;
        }
        for (int i = 0; i < GpPrintService.MAX_PRINTER_CNT; i++) {
            try {
                if (mGpService.getPrinterConnectStatus(i) == GpDevice.STATE_CONNECTED) {
                    state[i] = true;
                }
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return state;
    }

}
